package com.ruyuan.o2o.groupbuy.common.log.interceptor;

import org.slf4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * 日志调用拦截器
 *
 * @author ming qian
 */
@SuppressWarnings("all")
public class InvocationIdInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String invocationId = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
        //  提供给日志获取invocationId调用id
        MDC.put("INVOCATION_ID", invocationId);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        MDC.clear();
    }
}
